from django.http import HttpResponse
from django.shortcuts import render
from .models import Department, Student, Course, Stu_detail


# Create your views here.
def add(request):
    # # 学院表
    # Department.objects.get_or_create(d_name="理学院")
    # Department.objects.get_or_create(d_name="光电")
    # Department.objects.get_or_create(d_name="计算机")
    # Department.objects.get_or_create(d_name="通信")
    # Department.objects.get_or_create(d_name="生物学院")
    # # 学生表
    # Student.objects.get_or_create(s_name="wjj", d_id_id=2)
    # Student.objects.get_or_create(s_name="jj", d_id_id=1)
    # Student.objects.get_or_create(s_name="aj", d_id_id=3)
    # Student.objects.get_or_create(s_name="bj", d_id_id=4)
    # Student.objects.get_or_create(s_name="cj", d_id_id=5)
    # 学生详情表
    # Stu_detail.objects.get_or_create(sd_sex=1, sd_age=18, intro="这是阿杰", s_id_id=1)
    # Stu_detail.objects.get_or_create(sd_sex=1, sd_age=18, intro="这是jj", s_id_id=2)
    # Stu_detail.objects.get_or_create(sd_sex=0, sd_age=18, intro="这是阿佳", s_id_id=3)
    # Stu_detail.objects.get_or_create(sd_sex=0, sd_age=18, intro="这是bj", s_id_id=4)
    # Stu_detail.objects.get_or_create(sd_sex=0, sd_age=18, intro="这是cj", s_id_id=5)
    # # 课程表
    # Course.objects.get_or_create(c_name='高数')
    # Course.objects.get_or_create(c_name='大物')
    # Course.objects.get_or_create(c_name='python')
    # Course.objects.get_or_create(c_name='c++')
    # Course.objects.get_or_create(c_name='数据结构')
    # # 获取学生和课程对象
    c1 = Course.objects.get(id=1)
    c2 = Course.objects.get(id=2)
    c3 = Course.objects.get(id=3)
    c4 = Course.objects.get(id=4)
    c5 = Course.objects.get(id=5)
    s1 = Student.objects.get(s_id=1)
    s2 = Student.objects.get(s_id=2)
    s3 = Student.objects.get(s_id=3)
    s4 = Student.objects.get(s_id=4)
    s5 = Student.objects.get(s_id=5)
    # s1.course_set.add(c1, c4)  # 学生选课 反向访问
    # c2.c_id.add(s2, s3)  # 课选学生 正向访问
    #
    s2.course_set.create(c_name="web")
    return HttpResponse("添加数据成功")


def query(request):
    c1 = Course.objects.get(id=1)
    c2 = Course.objects.get(id=2)
    c3 = Course.objects.get(id=3)
    c4 = Course.objects.get(id=4)
    c5 = Course.objects.get(id=5)
    s1 = Student.objects.get(s_id=1)
    s2 = Student.objects.get(s_id=2)
    s3 = Student.objects.get(s_id=3)
    s4 = Student.objects.get(s_id=4)
    s5 = Student.objects.get(s_id=5)
    d1 = Department.objects.get(d_id=1)
    d2 = Department.objects.get(d_id=2)
    d3 = Department.objects.get(d_id=3)
    d4 = Department.objects.get(d_id=4)
    d5 = Department.objects.get(d_id=5)
    print(s1.d_id.d_name)  # 正向访问
    print(d1.student_set.all()[0].s_name)  # 反向访问
    return HttpResponse("查询数据成功")
